package com.wyl.home.video

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.google.android.material.tabs.TabLayout
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.openVideoDetailPage
import com.wyl.home.R
import com.wyl.home.databinding.VideoListActivityBinding
import com.wyl.libbase.adapter.SimpleTabSelectedListener
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.extend.autoWired
import io.ditclear.bindingadapter.ItemClickPresenter
import io.ditclear.bindingadapter.SingleTypeAdapter
import org.koin.android.viewmodel.ext.android.viewModel

class VideoListActivity : BindingActivity<VideoListActivityBinding>(), ItemClickPresenter<Map<String, String>> {
    private val title by lazy { autoWired("title", "列表") }

    private val viewModel: VideoListViewModel by viewModel()

    override fun getLayoutId(): Int = R.layout.video_list_activity

    override fun initView() {
        binding.vm = viewModel

        binding.titlebar.setListener { _, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                else -> {
                }
            }
        }
        binding.titlebar.centerTextView.text = title

        binding.tab.addOnTabSelectedListener(object : SimpleTabSelectedListener() {
            override fun onTabSelected(tab: TabLayout.Tab) {
                startRefresh(tab.text.toString())
            }
        })

        binding.include.recyclerView.apply {
            layoutManager = GridLayoutManager(context, 3)
            adapter = mAdapter
//            addItemDecoration(RecyclerViewDivider(context, RecyclerViewDivider.BOTH, dp2px(8f), Color.WHITE))
        }
    }

    private val mAdapter by lazy {
        SingleTypeAdapter(this, R.layout.home_item_video_list, viewModel.dataSource)
            .apply {
                itemPresenter = this@VideoListActivity
            }
    }

    override fun onItemClick(v: View, item: Map<String, String>) {
        openVideoDetailPage(item["appFolkOperaId"])
    }

    override fun loadData() {
        startRefresh("按热度")
    }

    private fun startRefresh(type: String) {
        viewModel.loadInit(title, type)
        binding.include.refreshLayout.startRefresh()
    }


}
